package com.eugenes.design.cardvalidator;

import com.eugenes.design.Card;

public class SingleCountCardValidator implements CardValidator {

    static final int DEFAULT_COUNTS_LEFT = 1;

    private Card card;
    int countsLeft;

    public SingleCountCardValidator(Card card, int counts) {
        if (counts < 0) {
            throw new IllegalArgumentException("Coutns should be greater or equal 0.");
        }
        ifNullThrowIllegalArgException(card);

        this.card = card;
        this.countsLeft = counts;
    }

    public SingleCountCardValidator(Card card) {
        this(card, DEFAULT_COUNTS_LEFT);
    }

    private void ifNullThrowIllegalArgException(Object obj) {
        if (obj == null) {
            throw new IllegalArgumentException("Argument is null.");
        }
    }

    @Override
    public boolean validate(Card card) {
        ifNullThrowIllegalArgException(card);
        return card == this.card && countsLeft > 0;
    }

    @Override
    public void cardIsUsed(Card card) {
        ifNullThrowIllegalArgException(card);
        if (card == this.card) {
            --countsLeft;
        }
    }
}
